package com.molyfun.parents.activity.report.view;

import android.content.Context;
import android.support.v4.app.FragmentManager;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.molyfun.parents.R;
import com.molyfun.parents.rest.bean.CourseDetailResponse;
import com.molyfun.parents.rest.bean.ReportDetailResponse;
import com.molyfun.parents.utils.TimeUtils;

import java.util.Calendar;

import butterknife.ButterKnife;
import butterknife.InjectView;
import cn.carbs.android.expandabletextview.library.ExpandableTextView;

import static cn.carbs.android.expandabletextview.library.ExpandableTextView.STATE_SHRINK;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:12/13/16.
 */
public class ReportDetailView extends LinearLayout {

    private static final int ITEM_COUNT_ONE = 0x00000001;

    protected LayoutInflater layoutInflater;

    private Context context;

    @InjectView(R.id.report_time)
    TextView mReportTime;
    @InjectView(R.id.report_course_list)
    LinearLayout mReportCourseList;
    @InjectView(R.id.report_summary)
    ExpandableTextView mSummary;
    @InjectView(R.id.report_summary_layout)
    LinearLayout mReportSummaryLayout;

    private Calendar mCalendar;

    ReportDetailResponse.ReportCourseBean mReportCourseBean;
    FragmentManager mFragmentManager;

    private int mListPosition = -1;

    public ReportDetailView(Context context, AttributeSet attrs, int defStyle) {
        this(context, attrs);
    }

    public ReportDetailView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public ReportDetailView(Context context) {
        super(context);
        init(context, null);
    }


    private void init(Context context, AttributeSet attrs) {
        this.context = context;
        layoutInflater = LayoutInflater.from(context);
        layoutInflater.inflate(R.layout.widget_report_detail, this);
        ButterKnife.inject(this);
        mCalendar = Calendar.getInstance();
    }

    public static interface OnReportDetailItemClick{
        void onDetailItemClick(int position, int showInfoInt);
    }

    private ReportProcessView.OnReportProcessItemClick mOnReportProcessItemClick;

    /**
     * init data
     * @param reportCourseBean
     */
    public void initData(int position, ReportDetailResponse.ReportCourseBean reportCourseBean,final int showInfoInt,final OnReportDetailItemClick onReportDetailItemClick) {

        mListPosition = position;
        if (reportCourseBean == null) {
            return;
        }

        mReportCourseBean = reportCourseBean;
        mCalendar.setTimeInMillis(mReportCourseBean.getPlandatetime());
        StringBuilder sb = new StringBuilder();
        sb.append(TimeUtils.getDateByTimeMillis(mReportCourseBean.getPlandatetime()));
        sb.append("(");
        sb.append(TimeUtils.getWeekDayName(mCalendar.get(Calendar.DAY_OF_WEEK)));
        sb.append(")");
        mReportTime.setText(sb.toString());
        if(TextUtils.isEmpty(mReportCourseBean.getCoursesummarycontent())){
            mReportSummaryLayout.setVisibility(View.GONE);
        }else{
            mReportSummaryLayout.setVisibility(View.VISIBLE);
            mSummary.updateForRecyclerView(mReportCourseBean.getCoursesummarycontent(), mSummary.getWidth(), STATE_SHRINK);
        }
        mReportCourseList.removeAllViews();
        int itemPosition = 0;
        for(CourseDetailResponse.CourseDetailBean courseDetailBean : reportCourseBean.getCoursesummarylist()){
            ReportProcessView reportProcessView = new ReportProcessView(context);
            final int itemPositionFinal = itemPosition;
            reportProcessView.initData(itemPosition, courseDetailBean, (showInfoInt & (ITEM_COUNT_ONE << itemPositionFinal)) > 0, new ReportProcessView.OnReportProcessItemClick() {
                @Override
                public void onItemClick(int position) {
                    onReportDetailItemClick.onDetailItemClick(mListPosition, showInfoInt ^ (ITEM_COUNT_ONE << itemPositionFinal));
                }
            });
            mReportCourseList.addView(reportProcessView);
            itemPosition ++;
        }

    }

}